<template>
    <div class="listDiv pd10">
        <div class="searchDiv">
            <el-form class="searchLeft" ref="serform" :model="obj.req" label-position="top">
                <el-form-item class="serlabelB mr20" label="产品名称：" prop="title">
                    <el-input v-model="obj.req.title" placeholder="请输入产品名称" class="search_key" clearable />
                </el-form-item>
                <el-form-item class="serlabelB mr20" label="货号：" prop="itemnumber">
                    <el-input v-model="obj.req.itemnumber" placeholder="请输入货号" class="search_key" clearable />
                </el-form-item>
                <el-form-item class="serlabelB mr20" label="厂家名称：" prop="manufactor">
                    <el-input v-model="obj.req.manufactor" placeholder="请输入厂家名称" class="search_key" clearable />
                </el-form-item>
                <el-form-item class="serlabelB mr20" label="品牌名称：" prop="brand">
                    <el-input v-model="obj.req.brand" placeholder="请输入品牌名称" class="search_key" clearable />
                </el-form-item>
                <el-form-item class="serlabelB mr20" label="产品用途：" prop="usage">
                    <el-select v-model="obj.req.usage" placeholder="请选择产品用途" style="width: 100px;">
                        <el-option v-for="(item,index) in obj.config.产品用途||[]" :key="index" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item class="mr20">
                    <el-button type="primary" @click="obj.req.currentpage=1;init()">查询</el-button>
                    <el-button plain @click="proxy.$refs['serform'].resetFields();init()">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table :data="obj.ary" style="width: 100%;flex: 1;"
            :header-cell-style="{ background: '#E5E6EB', color: '#333333' }">
            <el-table-column prop="title" label="产品名称"></el-table-column>
            <el-table-column prop="itemnumber" label="货号" width="140"></el-table-column>
            <el-table-column prop="min_price" label="最低单价" width="100"></el-table-column>
            <el-table-column prop="manufactor" label="厂家名称"></el-table-column>
            <el-table-column prop="brand" label="品牌名称" width="120"></el-table-column>
            <el-table-column prop="usage" label="产品用途" width="100"></el-table-column>
            <el-table-column prop="contacts" label="联系人" width="120"></el-table-column>
            <el-table-column prop="mobile" label="联系电话" width="100"></el-table-column>
            <el-table-column prop="verifyText" label="审核状态" width="100">
                <template #default="scope">
                    <el-tag :type="scope.row.verifyType">{{ scope.row.verifyText }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="" label="操作" width="80">
                <template #default="scope">
                    <el-button type="primary" size="small" @click="showVerify(scope.row)">审核</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="flex-center mt10">
            <el-pagination
                background
                size="small"
                @size-change="init()"
                @current-change="init()"
                :current-page.sync="obj.req.currentpage"
                :page-sizes="[10, 20, 50, 100]"
                :page-size.sync="obj.req.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="obj.req.count">
                </el-pagination>
        </div>

        <el-drawer v-model="obj.drawer.show" :title="obj.drawer.title" size="50%" direction="rtl" :close-on-click-modal="false" destroy-on-close>
            <ProductEdit v-model:id="obj.drawer.product_id" @init="init" @close="obj.drawer.show = false" />
        </el-drawer>

        <el-dialog v-model="obj.dialog.show" :title="obj.dialog.title" width="400px" :close-on-click-modal="false" destroy-on-close>
            <el-form ref="verifyForm" :model="obj.dialog.verifyForm" label-width="80px">
                <el-form-item label="审核结果" prop="verify" :rules="[formRulesMixin.notNull]">
                    <el-select v-model="obj.dialog.verifyForm.verify" placeholder="请选择审核状态">
                        <template v-for="(item,index) in proxy.$setting.globalData.verifyTypeAry" :key="index">
                            <el-option v-if="item.id!=0" :label="item.text_" :value="item.id" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item v-if="obj.dialog.verifyForm.verify==2" label="拒绝原因" prop="verify_msg" :rules="[formRulesMixin.notNull]">
                    <el-input v-model="obj.dialog.verifyForm.verify_msg" type="textarea" placeholder="请输入拒绝原因" maxlength="50" clearable autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click="obj.dialog.show=false">取消</el-button>
                    <el-button type="primary" @click="submitVerify">提交</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script setup>
    import { reactive,getCurrentInstance,onMounted } from 'vue'
    import ProductEdit from './productEdit.vue'
    let { proxy } = getCurrentInstance()

    let obj = reactive({
        drawer: {
            show: false,
            title: "",
            product_id: 0
        },
        req:{
            "title": "",
            "itemnumber": "",
            "manufactor": "",
            "brand": "",
            "usage": "",
            "verify": 0,
            "shelves": "",
            currentpage: 1,
            pagesize: 10,
            count: 0,
            
        },
        ary:[],
        config: {},
        dialog: {
            show: false,
            title: '',
            verifyForm: {
                "verify": "",
                "verify_msg": "",
                "product_id": ""
            }
        },
    })

    onMounted(async()=>{
        obj.config = await proxy.$setting.getConfig()
        init()
    })

    let init = () => {
        proxy.$ajax({
            url: '/manager/product/list',
            data: obj.req,
            bfLoading: true
        }).then(d => {
            if(d.success){
                obj.ary = d.msg.list
                obj.ary.map(e=>{
                    e.verifyText = proxy.$util.aryVal("verifyTypeAry", e.verify, 'text')
                    e.verifyType = proxy.$util.aryVal("verifyTypeAry", e.verify, 'colorType')
                    e.shelvesText = proxy.$util.aryVal("shelvesTypeAry", e.shelves, 'text')
                    e.shelvesType = proxy.$util.aryVal("shelvesTypeAry", e.shelves, 'colorType')
                })
                obj.req.count = d.msg.count
            }
        }).catch(() => { })
    }
    
    const showVerify=(row)=>{
        obj.dialog = {
            show: true,
            title: `审核${row.title}`,
            verifyForm: {
                "verify": "",
                "verify_msg": "",
                "product_id": row.product_id
            }
        }
    }

    const submitVerify=()=>{
        proxy.$refs['verifyForm'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: '/manager/product/verify',
                    data: obj.dialog.verifyForm,
                    bfLoading: true
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin("审核成功", 'success')
                        obj.dialog.show = false
                        init()
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }
        })
    }
    
</script>

<style scoped>
</style>